using System;
using OpenTK;
namespace Foundry.Engine
{
	public class Camera : Node
	{
		public Camera ()
		{
			abs_cache=Matrix4.Identity;
			aspect = 1f;
			fov= (float)Math.PI / 3;
			near=.1f;
			far=1000f;
			changed=true;
		}
		
		private bool changed;
		
		private float aspect;
		private float fov;
		private float near;
		private float far;
		private Matrix4 projection;
		
		public float AspectRatio { get { return aspect; } set { aspect = value; changed = true; } }
		public float FieldOfView { get { return fov; } set { fov = value; changed = true; } }
		public float Far { get { return far; } set { far = value; changed = true; } }
		public float Near { get { return near; } set { near =value; changed = true; } }
		public Vector3 Up { get; set; }
		public Transform Target { get; set; }
		
		private void GenerateProjection()
		{
			projection = Matrix4.CreatePerspectiveFieldOfView(fov, aspect, near, far);
			//projection = Matrix4.CreateOrthographic(4, 4, -4, 4);
			changed = false;
		}
		
		public Matrix4 Projection { get { if(changed) GenerateProjection(); return projection; } }
		
		//public Matrix4 View { get { var t = AbsoluteTransform; t.Invert(); return t; } }
		
		public void Step()
		{
			if(Target != null)
			{
				var mat = Matrix4.LookAt(AbsolutePosition, Target.AbsolutePosition, Up);
				var pr= Parent.AbsoluteRotation;
				pr.Conjugate();
				// Remove any inherited rotation
				RelativeRotation = mat.ToQuaternion() * pr;
			}
		}
		
	}
}

